﻿class Solution
{
public:
	//将问题转化  变成找数组和sum-x 最长 (n - len)    target = sum - x;
	int minOperations(vector<int>& nums, int x)
	{
		int n = nums.size();
		int sum = accumulate(nums.begin(), nums.end(), 0);//求数组中的和
		int target = sum - x;//连续数组的子数组的和
		int sumnum = 0;
		int len = -1;
		if (target < 0) return -1;
		for (int left = 0, right = 0; right < n; right++)
		{
			sumnum += nums[right];
			while (sumnum > target)
			{
				sumnum -= nums[left++];
			}
			if (sumnum == target)
			{
				len = max(len, right - left + 1);
			}
		}
		return len == -1 ? -1 : n - len;
	}
};